package com.wut.clock.controller;

import com.wut.clock.aop.log.BusinessType;
import com.wut.clock.aop.log.Log;
import com.wut.clock.common.api.CommonPage;
import com.wut.clock.common.api.CommonResult;
import com.wut.clock.service.IArticleService;
import com.wut.clock.vo.admin.ArticleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: yuwei
 * @Date: 2022/5/31 20:31
 * @Description: 文章管理
 */
@Slf4j
@Api(value="文章管理器",tags = {"文章管理器"})
@RestController
@RequiredArgsConstructor
@RequestMapping("/article")
public class ArticleController {

    private final IArticleService articleService;

    @ApiOperation("删除文章")
    @PostMapping("/deleteArticle/{articleId}")
    @RequiresPermissions("am:article:delete")
    @Log(title = "删除文章",businessType = BusinessType.DELETE)
    public CommonResult<String> deleteArticle(@PathVariable Integer articleId) {
        if(articleService.deleteArticle(articleId)>0) return CommonResult.success();
        return CommonResult.failed();
    }

    @ApiOperation("查看文章列表")
    @GetMapping("/getArticleList")
    @RequiresPermissions("am:article:query")
    public CommonResult<CommonPage<ArticleVo>> getArticleList(@RequestParam(defaultValue = "1") Integer pageNum,
                                                              @RequestParam(defaultValue = "10") Integer pageSize,
                                                              @RequestParam(required = false) Integer userId,
                                                              @RequestParam(required = false) Integer categoryId,
                                                              @RequestParam(required = false) String articleTitle,
                                                              @RequestParam(required = false) Integer status,
                                                              @RequestParam(required = false) Integer verifyStatus) {
        List<ArticleVo> articleList = articleService.getArticleList(pageNum,pageSize,userId,categoryId,articleTitle,status,verifyStatus);
        if(articleList.size()>0){
            return CommonResult.success(CommonPage.restPage(articleList),"查看文章列表成功");
        }
        return CommonResult.failed("文章列表不存在");
    }

    @ApiOperation("查看文章详情")
    @GetMapping("/getArticleDetail")
    @RequiresPermissions("am:article:query")
    public CommonResult<ArticleVo> getArticleDetail(@RequestParam Integer articleId) {
        ArticleVo article = articleService.getArticleDetail(articleId);
        if(article != null) return CommonResult.success(article);
        return CommonResult.failed("该文章不存在");
    }

}
